// pages/grid1_rule.js
const getPlanFilesByKeyword = require('../../utils/API').getPlanFilesByKeyword
const getLatestPlanFiles = require('../../utils/API').getLatestPlanFiles
const getResourceUrl = require('../../utils/API').getResourceUrl
Page({
    data: {
        keyword: '',
        fileList: []
    },
    onLoad() {
        getLatestPlanFiles().then(
            files => {
                this.setData({
                    fileList: files
                })
            }, e => {
                wx.showToast({
                    title: '网络请求错误',
                    icon: 'none'
                })
            }
        )
    },
    bindKeywordInput(e) {
        this.setData({
            keyword: e.detail.value
        })
    },
    // 点击文件时  打开文件
    bindTapFile(e) {
        let link = e.target.dataset.link
        let url = getResourceUrl(link)
        wx.downloadFile({
            url,
            success: function (res) {
                const filePath = res.tempFilePath
                wx.openDocument({
					filePath,
					// fileType: 'doc',
                    success: function (res) {
                        console.log('打开文档成功')
                    },
					fail(e){
						console.log(e)
					}
                })
            },
            fail() {
                wx.showToast({
                    title: '打开文件失败',
                    icon: 'none'
                })
            }
        })
    },
    // 点击搜索按钮
    bindSearchButtonTap() {
        getPlanFilesByKeyword(this.data.keyword).then(
            files => {
                this.setData({
                    fileList: files
                })
            }, e => {
                wx.showToast({
                    title: '网络请求错误',
                    icon: 'none'
                })
            })
    }
})